﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace AI
{
	// Token: 0x020001EF RID: 495
	public class BehaviorController
	{
		// Token: 0x06000A82 RID: 2690 RVA: 0x0005ACFC File Offset: 0x00058EFC
		public BehaviorController(EntityBase entity)
		{
			this.commandQueue = new Queue<BehaviorController.Command>();
			this.secondaryBehaviors = new List<BehaviorController.Command>();
			this.actionController = entity.actionManager;
			this.agent = entity;
		}

		// Token: 0x06000A83 RID: 2691 RVA: 0x0005AD2D File Offset: 0x00058F2D
		public string GetCurrentBehaviorName()
		{
			if (this.mainBehavior != null)
			{
				return this.mainBehavior.behavior.GetName();
			}
			return null;
		}

		// Token: 0x06000A84 RID: 2692 RVA: 0x0005AD4C File Offset: 0x00058F4C
		private BehaviorController.Command CreateCommand(BehaviorInstruction behaviorInstruction)
		{
			return new BehaviorController.Command
			{
				behavior = behaviorInstruction.newBehavior,
				target = behaviorInstruction.target,
				cursorPoint = behaviorInstruction.cursorPoint.ToVirtual(),
				agent = this.agent
			};
		}

		// Token: 0x06000A85 RID: 2693 RVA: 0x0005AD98 File Offset: 0x00058F98
		public void SetBehaviorByName(string name, EntityBase target, Vector3 point)
		{
			if (BehaviorLists.Instance == null)
			{
				return;
			}
			IBehavior behavior = BehaviorLists.Instance.GetBehavior(name);
			if (behavior == null)
			{
				return;
			}
			this.ChangeBehavior(new BehaviorInstruction
			{
				newBehavior = behavior,
				target = target,
				cursorPoint = point
			});
		}

		// Token: 0x06000A86 RID: 2694 RVA: 0x0005ADEC File Offset: 0x00058FEC
		public void QueueBehaviorByName(string name, EntityBase target, Vector3 point)
		{
			if (BehaviorLists.Instance == null)
			{
				return;
			}
			IBehavior behavior = BehaviorLists.Instance.GetBehavior(name);
			if (behavior == null)
			{
				return;
			}
			this.ScheduleBehavior(new BehaviorInstruction
			{
				newBehavior = behavior,
				target = target,
				cursorPoint = point
			});
		}

		// Token: 0x06000A87 RID: 2695 RVA: 0x0005AE3E File Offset: 0x0005903E
		public void ChangeBehavior(BehaviorInstruction behaviorInstruction)
		{
			this.commandQueue.Clear();
			if (!behaviorInstruction.newBehavior.IsSecondary() && this.mainBehavior != null)
			{
				this.StopMainBehavior();
			}
			this.ScheduleBehavior(behaviorInstruction);
		}

		// Token: 0x06000A88 RID: 2696 RVA: 0x0005AE74 File Offset: 0x00059074
		public void ScheduleBehavior(BehaviorInstruction behaviorInstruction)
		{
			BehaviorController.Command item = this.CreateCommand(behaviorInstruction);
			this.commandQueue.Enqueue(item);
		}

		// Token: 0x06000A89 RID: 2697 RVA: 0x0005AE95 File Offset: 0x00059095
		public void Execute()
		{
			this.CheckQueueForBehaviors();
			this.ExecuteMainBehavior();
			this.ExecuteSecondaryActions();
		}

		// Token: 0x06000A8A RID: 2698 RVA: 0x0005AEA9 File Offset: 0x000590A9
		public void FixedExecute()
		{
			this.FixedExecuteMainBehavior();
			this.FixedExecuteSecondaryActions();
		}

		// Token: 0x06000A8B RID: 2699 RVA: 0x0005AEB8 File Offset: 0x000590B8
		private void CheckQueueForBehaviors()
		{
			if (this.commandQueue.Count == 0)
			{
				return;
			}
			BehaviorController.Command command = this.commandQueue.Peek();
			if (command.IsSecondary())
			{
				command = this.commandQueue.Dequeue();
				this.RunSecondaryBehavior(command);
			}
			else if (this.HasMainBehavior())
			{
				this.mainBehavior = this.commandQueue.Dequeue();
			}
		}

		// Token: 0x06000A8C RID: 2700 RVA: 0x0005AF24 File Offset: 0x00059124
		private void ExecuteMainBehavior()
		{
			if (this.mainBehavior == null)
			{
				return;
			}
			this.mainBehavior.Execute();
			if (this.mainBehavior == null)
			{
				return;
			}
			if (this.mainBehavior.AutoFinish() && this.actionController.IsIdle())
			{
				this.StopMainBehavior();
				return;
			}
		}

		// Token: 0x06000A8D RID: 2701 RVA: 0x0005AF7C File Offset: 0x0005917C
		private void FixedExecuteMainBehavior()
		{
			if (this.mainBehavior == null)
			{
				return;
			}
			this.mainBehavior.FixedUpdate();
			if (this.mainBehavior == null)
			{
				return;
			}
			if (this.mainBehavior.AutoFinish() && this.actionController.IsIdle())
			{
				this.StopMainBehavior();
				return;
			}
		}

		// Token: 0x06000A8E RID: 2702 RVA: 0x0005AFD4 File Offset: 0x000591D4
		private void ExecuteSecondaryActions()
		{
			for (int i = 0; i < this.secondaryBehaviors.Count; i++)
			{
				this.secondaryBehaviors[i].Execute();
			}
		}

		// Token: 0x06000A8F RID: 2703 RVA: 0x0005B010 File Offset: 0x00059210
		private void FixedExecuteSecondaryActions()
		{
			for (int i = 0; i < this.secondaryBehaviors.Count; i++)
			{
				this.secondaryBehaviors[i].FixedUpdate();
			}
		}

		// Token: 0x06000A90 RID: 2704 RVA: 0x0005B04C File Offset: 0x0005924C
		private void RunSecondaryBehavior(BehaviorController.Command command)
		{
			foreach (string flag in command.GetFlags())
			{
				this.StopSecondaryBehavior(flag);
			}
			this.secondaryBehaviors.Add(command);
		}

		// Token: 0x06000A91 RID: 2705 RVA: 0x0005B0B4 File Offset: 0x000592B4
		public void StopSecondaryBehavior(string flag)
		{
			foreach (BehaviorController.Command command in this.secondaryBehaviors)
			{
				foreach (string b in command.GetFlags())
				{
					if (flag == b)
					{
						if (GlobalPreferences.ScriptAuxLogging.value)
						{
							Debug.Log("Flag match: " + flag);
						}
						this.secondaryBehaviors.Remove(command);
						command.Exit();
						return;
					}
				}
			}
		}

		// Token: 0x06000A92 RID: 2706 RVA: 0x0005B190 File Offset: 0x00059390
		public void StopMainBehavior()
		{
			if (this.mainBehavior != null)
			{
				this.actionController.ClearAll();
				this.mainBehavior.Exit();
				this.mainBehavior = null;
			}
		}

		// Token: 0x06000A93 RID: 2707 RVA: 0x0005B1BC File Offset: 0x000593BC
		public void StopAllBehaviors()
		{
			this.StopMainBehavior();
			foreach (BehaviorController.Command command in this.secondaryBehaviors)
			{
				command.Exit();
			}
			this.secondaryBehaviors.Clear();
		}

		// Token: 0x06000A94 RID: 2708 RVA: 0x0005B228 File Offset: 0x00059428
		public bool HasMainBehavior()
		{
			return this.mainBehavior == null;
		}

		// Token: 0x06000A95 RID: 2709 RVA: 0x0005B233 File Offset: 0x00059433
		public bool IsIdle()
		{
			return this.commandQueue.Count == 0 && this.HasMainBehavior();
		}

		// Token: 0x06000A96 RID: 2710 RVA: 0x0005B24E File Offset: 0x0005944E
		public bool IsQueueEmpty()
		{
			return this.commandQueue.Count == 0;
		}

		// Token: 0x06000A97 RID: 2711 RVA: 0x0005B25E File Offset: 0x0005945E
		public void ClearQueue()
		{
			this.commandQueue.Clear();
		}

		// Token: 0x04000FB6 RID: 4022
		private EntityBase agent;

		// Token: 0x04000FB7 RID: 4023
		private ActionController actionController;

		// Token: 0x04000FB8 RID: 4024
		private BehaviorController.Command mainBehavior;

		// Token: 0x04000FB9 RID: 4025
		private List<BehaviorController.Command> secondaryBehaviors;

		// Token: 0x04000FBA RID: 4026
		private Queue<BehaviorController.Command> commandQueue;

		// Token: 0x020001F0 RID: 496
		public class Command
		{
			// Token: 0x06000A99 RID: 2713 RVA: 0x0005B26B File Offset: 0x0005946B
			public void Execute()
			{
				if (!this.hasStarted)
				{
					this.Start();
				}
				this.Update();
			}

			// Token: 0x06000A9A RID: 2714 RVA: 0x0005B284 File Offset: 0x00059484
			public void Update()
			{
				this.instance.Update();
			}

			// Token: 0x06000A9B RID: 2715 RVA: 0x0005B291 File Offset: 0x00059491
			public void FixedUpdate()
			{
				this.instance.FixedUpdate();
			}

			// Token: 0x06000A9C RID: 2716 RVA: 0x0005B29E File Offset: 0x0005949E
			public void Exit()
			{
				this.instance.Exit();
			}

			// Token: 0x06000A9D RID: 2717 RVA: 0x0005B2AB File Offset: 0x000594AB
			public void Start()
			{
				this.instance = this.behavior.CreateInstance(this.agent, this.target, this.cursorPoint);
				this.instance.Start();
				this.hasStarted = true;
			}

			// Token: 0x06000A9E RID: 2718 RVA: 0x0005B2E2 File Offset: 0x000594E2
			public bool AutoFinish()
			{
				return this.instance.AutoFinish();
			}

			// Token: 0x06000A9F RID: 2719 RVA: 0x0005B2EF File Offset: 0x000594EF
			public bool IsSecondary()
			{
				return this.behavior.IsSecondary();
			}

			// Token: 0x06000AA0 RID: 2720 RVA: 0x0005B2FC File Offset: 0x000594FC
			public List<string> GetFlags()
			{
				return this.behavior.GetFlags();
			}

			// Token: 0x04000FBB RID: 4027
			public IBehavior behavior;

			// Token: 0x04000FBC RID: 4028
			public IBehaviorInstance instance;

			// Token: 0x04000FBD RID: 4029
			public Vector3 cursorPoint;

			// Token: 0x04000FBE RID: 4030
			public EntityBase target;

			// Token: 0x04000FBF RID: 4031
			public EntityBase agent;

			// Token: 0x04000FC0 RID: 4032
			private bool hasStarted;
		}
	}
}
